package engine

import (
	"log"
	"qweewq.tk/example/crawier/fetcher"
	"sync"
)

type Scheduler struct {
	Mxt sync.Mutex
	Items []Item

	wg sync.WaitGroup
}

func NewScheduler() *Scheduler {
	return &Scheduler{
		Mxt:   sync.Mutex{},
		Items: make([]Item, 1024),
	}
}

func (s *Scheduler) AddTask(req Request) {
	s.wg.Add(1)
	defer s.wg.Done()
	content, err := fetcher.Fetch(req.Url)
	if err != nil {
		log.Println(err)
		return
	}
	log.Println(req.Url)

	if req.ParserFunc != nil {
		parser := req.ParserFunc(content)
		if parser != nil {
			for _, r := range parser.Requests {
				go s.AddTask(r)
			}

			s.AddItem(parser.Items)
		}
	}
}

func (s *Scheduler) AddItem(items ...Item) {
	s.Mxt.Lock()
	defer s.Mxt.Unlock()
	s.Items = append(s.Items, items)
}

func (s *Scheduler) RunTask() {
	s.wg.Wait()
}